package jgl.vajra.management.part.mine.activity;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.management.R;
import jgl.vajra.management.bean.CarMemberBean;
import jgl.vajra.management.bean.MyCarListBean;
import jgl.vajra.management.databinding.ActivityMyCarSearchBinding;
import jgl.vajra.management.dialogFragment.CertificationAuditDialogFragment;
import jgl.vajra.management.listener.MessageDlialogListener;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.manager.FragmentManager;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.part.mine.contract.MyCarSearchContract;
import jgl.vajra.management.part.mine.viewModel.MyCarSearchViewModel;
import jgl.vajra.management.widget.bean.BaseListData;
import jgl.vajra.management.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.dialogfragment.MessageDialogFragment;
import jgl.vajra.management.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.management.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.management.widget.utils.ToastUtils;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(MyCarSearchViewModel.class)
public class MyCarSearchActivity extends BaseMVVMActivity<MyCarSearchViewModel, ActivityMyCarSearchBinding> implements MyCarSearchContract.View, BaseBindingItemPresenter<String> {

    public String whereEnd = "";
    private int tag;
    private CertificationAuditDialogFragment certificationAuditDialogFragment;
    private int internal;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_car_search;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.toolbar.setNavigationIcon(R.drawable.ic_left_arrow);
        mBinding.toolbar.setNavigationOnClickListener(view -> finish());

        tag = getIntent().getIntExtra("tag", 1);
        internal = getIntent().getIntExtra("internal", 0);

        mBinding.etText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if (keyCode == keyEvent.KEYCODE_ENTER) {
                    String etText = mBinding.etText.getText().toString().trim();
                    if (!TextUtils.isEmpty(etText)) {
                        whereEnd = "and (g.sphone like '%"+etText+"' OR a.logo like '%"+etText+"')";
                        mBinding.recyclerView.firstLoad();
                    }

                }

                return false;
            }
        });
        mBinding.recyclerView.setRefreshRecyclerNetConfig(new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
                map.put("type", 1);
                map.put("tag", tag);
                map.put("internal", internal);
                map.put("whereEnd", whereEnd);
                return mViewModel.getMyCarList(map);
            }
        });

        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_my_car_search);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<BaseListData<MyCarListBean>>() {
            @Override
            public void onSucceed(BaseListData<MyCarListBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(true, true);

                mBinding.recyclerView.requestNetObjectSuccess(data.data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
//                mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });

        //mBinding.recyclerView.firstLoad();

        mBinding.recyclerView.setRecyclerViewBackgroundRes(R.color.gray_f7);
        mBinding.recyclerView.setRootViewBackgroundRes(R.color.gray_f7);
    }


    @Override
    public void onDetails(MyCarListBean itemData) {
        IntentManager.toCarDetailsActivity(this, itemData.logo, itemData.model,tag);
    }

    //车贴申请
    @Override
    public void onApply(MyCarListBean itemData) {
        IntentManager.toMemberApplyActivity(this, itemData.logo);
    }

    @Override
    public void returnDeleteCarData() {
        ToastUtils.showShort("删除成功");
        mBinding.recyclerView.firstLoad();
    }

    @Override
    public void onItemClick(int position, String itemData) {

    }

    //审核
    @Override
    public void onDetermine(String logo) {
        certificationAuditDialogFragment = new CertificationAuditDialogFragment();
        certificationAuditDialogFragment.setData(tag, logo);
        certificationAuditDialogFragment.setCertificationAuditListener(new CertificationAuditDialogFragment.CertificationAuditListener() {
            @Override
            public void onDetermine() {
                finish();
            }
        });
        certificationAuditDialogFragment.show(getSupportFragmentManager());
    }
}
